package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@Transactional
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @Override
    public void insert(DishDTO dishDTO) {
        //添加参数到菜品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //设置菜品默认为停售
        dish.setStatus(StatusConstant.DISABLE);
        dishMapper.insertDish(dish);
        //添加参数到菜品口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size()>0){
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dish.getId());
            }
            dishFlavorMapper.insertDishFlavor(flavors);
        }
    }

    /**
     * 菜品分页查询
     * @param dto
     * @return
     */
    @Override
    public PageResult selectAll(DishPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Page<DishVO> page = dishMapper.selectAll(dto);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @Override
    public DishVO select(Long id){
        DishVO dishVO = new DishVO();
        //查询菜品
        Dish dish = dishMapper.select(id);

        //查询菜品口味
        List<DishFlavor> listDishFlavor = dishFlavorMapper.select(id);

        //合并
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(listDishFlavor);
        return dishVO;
    }

    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @Override
    public void update(DishDTO dishDTO) {
        //修改菜品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.updateDish(dish);

        //删除菜品口味
        dishFlavorMapper.delete(Arrays.asList(dishDTO.getId()));

        //添加菜品口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size()>0){
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dish.getId());
            }
            dishFlavorMapper.insertDishFlavor(flavors);
        }
    }

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @Override
    public void deleteAll(List<Long> ids) {
        //删除菜品
        //查看是否有属于起售的菜品，如果有，禁止删除 select * from dish where id in(ids) and status = 1
        List<Dish> listDish = dishMapper.selectStatus(ids);
        if (listDish != null && listDish.size() > 0){
            //说明有起售菜品 , 不能删除 , 抛出异常
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }

        //查看是否有关联的套餐,如果有,禁止删除  select * from setmeal_dish where dish_id in(ids)
        List<SetmealDish> listSetmealDish = setmealDishMapper.selectSetmealByIds(ids);
        if (listSetmealDish != null && listSetmealDish.size() > 0){
            //说明有关联套餐 , 不能删除 , 抛出异常
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品
        dishMapper.delete(ids);

        //删除菜品口味
        dishFlavorMapper.delete(ids);

    }

    /**
     * 根据分类Id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> selectCategory(Long categoryId) {
        List<Dish> listDish = dishMapper.selectCategory(categoryId);
        return listDish;
    }

    /**
     * 菜品起售,停售
     * @param status
     * @param id
     * @return
     */
    @Override
    public void setStatus(Integer status, Long id) {

        //设置起售,停售
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.updateDish(dish);

        //如果是停售, 需要将包含该菜品的套餐也停售
        if (status == StatusConstant.DISABLE){
            //停售套餐  update setmeal set status = #{status} where id in (select setmeal_id from setmeal_dish where dish_id = #{id})
            setmealMapper.setStatus(dish);
        }
    }

    /**
     * 根据分类Id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<DishVO> selectById(Long categoryId) {
        //查询菜品
        List<Dish> list = dishMapper.selectCategoryStatus(categoryId,StatusConstant.ENABLE);

        List<DishVO> dishVOList = new ArrayList<>();
        for (Dish dish : list) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish,dishVO);

            List<DishFlavor> dfList = dishFlavorMapper.select(dish.getId());
            dishVO.setFlavors(dfList);
            dishVOList.add(dishVO);
        }
        return dishVOList;
    }


}
